package database;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import dataPO.*;
import databaseInterface.EquiTicketManageInterface;


public class EquiTicketManage implements EquiTicketManageInterface{

	// the denotation of equi ticket should be more than 0; judged in client
		Statement stmt;
		DatabaseConnection db= new DatabaseConnection();
		
		public void connect (){        
	         stmt =db.connect();
	    }
	    
		public void insert (EquivalentCouponsPO  eq){    	 
			String insert ="insert into equiTicket (equiDeno) values ("+eq.getDenomination()+")";
	        try {			
	        	stmt.executeUpdate(insert);
			} catch (SQLException e) {
				// TODO Auto-generated catch block			
				e.printStackTrace();
			}    
		}
	            	
		public void delete (double deno ){    	
	    	String delete="delete from equiTicket  where equiDeno = "+deno ;
	    	try {
				stmt.executeUpdate(delete);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
		    
		public void update (EquivalentCouponsPO eq, double deno){    	
	    	String update ="update equiTicket set equiDeno = "+eq.getDenomination()+" where equiDeno = "+deno ;
	        try {
				stmt.executeUpdate(update);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}    
	    }
	    
		public boolean exist(double deno){
	    	String query = "select * from equiTicket where equiDeno = "+String.valueOf(deno);
	    	ArrayList<EquivalentCouponsPO> tlist = search(query);
	    	if(tlist.isEmpty())
	    		return false ;
	    	
	    	return true ;	
	    }
		
		public ArrayList<EquivalentCouponsPO> query (){
	    	String query = "select * from equiTicket  ";
	    	ArrayList<EquivalentCouponsPO> tlist = search(query);
	    	return tlist;	
	    }
	  
		public ArrayList<EquivalentCouponsPO> search(String str){
	    	ArrayList<EquivalentCouponsPO> tlist = new ArrayList<EquivalentCouponsPO> ();
	     	try {
				ResultSet rs= stmt.executeQuery(str);
				while(rs.next()){
		    		EquivalentCouponsPO q= toEquivalentCouponsPO(rs);
		    	    tlist.add(q);
		    	
		    	}		    	    	
	    	} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	
	    	return tlist;	
	    }
		
	    private EquivalentCouponsPO toEquivalentCouponsPO(ResultSet rs) {
		// TODO Auto-generated method stub
	    	EquivalentCouponsPO eq= new EquivalentCouponsPO();
	    	try {
	    		eq.setDenomination(rs.getDouble("equiDeno"));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	    	return eq;
	    }
	  
	   

	    public void close(){      
	    	db.close();
	    }
	 
	   

}

		